﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using Oracle.DataAccess.Client;

namespace XMK.Framework.DataAccess
{
    /// <summary>
    /// Oracle-Odp数据库驱动
    /// </summary>
    public partial class OdpDbProvider : DbProvider
    {
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="connString">数据库连接字符串</param>
        public OdpDbProvider(string connString)
        {
            this.ConnString = connString;
        }
        /// <summary>
        /// 实例化数据库操作命令
        /// </summary>
        /// <returns>数据库操作命令</returns>
        public override IDbCommand CreateCommand()
        {
            return new OracleCommand();
        }
        /// <summary>
        /// 实例化数据库连接
        /// </summary>
        /// <returns>数据库连接</returns>
        public override IDbConnection CreateConnection()
        {
            return new OracleConnection();
        }
        /// <summary>
        /// 实例化数据库适配器
        /// </summary>
        /// <returns>数据库适配器</returns>
        public override IDbDataAdapter CreateDataAdapter()
        {
            return new OracleDataAdapter();
        }
        /// <summary>
        /// 实例化数据库参数
        /// </summary>
        /// <returns>数据库操作的参数</returns>
        public override IDbDataParameter CreateParameter()
        {
            return new OracleParameter();
        }
        /// <summary>
        /// 取得参数的前缀
        /// </summary>
        public override string ParamPrefix
        {
            get { return "p_"; }
        }

        /// <summary>
        /// 开始数据库缓存依赖
        /// </summary>
        public override void DbDependencyStart()
        {
        }
        /// <summary>
        /// 结束数据库缓存依赖
        /// </summary>
        public override void DbDependencyStop()
        {
        }
        /// <summary>
        /// 创建数据库依赖
        /// </summary>
        /// <typeparam name="T">数据库依赖保存的列表类型</typeparam>
        /// <param name="sqlText">数据库依赖查询字符串</param>
        /// <param name="fun">反序列化类型委托</param>
        /// <returns>数据库依赖实现类</returns>
        public override DbDependency<T> CreateDbDependency<T>(string sqlText, DataRowFullInfoFun<T> fun)
        {
            return new OdpDbDependency<T>(this.ConnString, sqlText,fun);
        }
        /// <summary>
        /// 调用存储过程获取参数信息
        /// </summary>
        /// <param name="cmd">存储过程命令</param>
        /// <param name="dbCmdInfo">命令信息</param>
        internal override void DeriveParameters(IDbCommand cmd, DbCmdInfo dbCmdInfo)
        {
            if (cmd.CommandType == CommandType.Text)
            {
                // 命令语句替换
                cmd.CommandText = cmd.CommandText.Replace("@", ":");
                // 参数前缀替换
                for (int i = 0; i < dbCmdInfo.Params.Count; i++)
                {
                    dbCmdInfo.Params[i].ParameterName = dbCmdInfo.Params[i].ParameterName.Replace("@", ":");
                    cmd.Parameters.Add(dbCmdInfo.Params[i]);
                }
            }
            else if (cmd.CommandType == CommandType.StoredProcedure)
            {
                OracleCommandBuilder.DeriveParameters(cmd as OracleCommand);
                for (int j = 0; j < dbCmdInfo.Params.Count; j++)
                {
                    ((IDbDataParameter)cmd.Parameters[dbCmdInfo.Params[j].ParameterName]).Value = dbCmdInfo.Params[j].Value;
                    if (dbCmdInfo.Params[j].Direction != ParameterDirection.Input)
                    {
                        //为了能够获取返回参数，此处将更新本地存储的参数信息
                        dbCmdInfo.Params[j] = (IDbDataParameter)cmd.Parameters[dbCmdInfo.Params[j].ParameterName];
                    }
                }
            }
        }
    }
}
